package com.jinghua.treatment.common.exception;

import com.jinghua.treatment.common.GlobalConstant;
import com.jinghua.treatment.common.domain.JSONResult;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@ResponseBody
public class GlobalException {

    Logger logger = Logger.getLogger(GlobalException.class);
    /**
     * 所有异常报错
     * @param request
     * @param exception
     * @return
     * @throws Exception
     */
    @ExceptionHandler(value=Exception.class)
    public JSONResult allExceptionHandler(HttpServletRequest request,
                                          Exception exception) throws Exception
    {
        exception.printStackTrace();
        logger.error("error happend!"+exception.getMessage());
        if(exception instanceof BizException){
            BizException bizException  = (BizException)exception;
            return new JSONResult(GlobalConstant.FAIL_CODE,bizException.getMessage());
        }
        return new JSONResult(GlobalConstant.FAIL_CODE,"服务器忙 ，请稍后再试",exception.getMessage());
    }

}
